<template>
  <ContentWrap>
    <div class="btnWrap routeHeader">
      <div>
        <el-button @click="close">关闭</el-button>
      </div>
    </div>
  </ContentWrap>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="到货单号" prop="arriveNumber">
        <el-input v-model="queryParams.arriveNumber" clearable class="!w-240px" disabled />
      </el-form-item>
      <el-form-item label="签收人" prop="signer">
        <el-input v-model="queryParams.signer" clearable class="!w-240px" disabled />
      </el-form-item>
      <el-form-item label="到货日期" prop="arriveTime">
        <el-date-picker
          v-model="queryParams.arriveTime"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="选择日期"
          class="!w-220px"
          disabled
        />
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      ref="multipleTableRef"
      border
      :summary-method="getSummaries"
      show-summary
      row-key="id"
      :height="tableHeight"
      :tree-props="{ children: 'children' }"
      default-expand-all
      v-loading="loading"
      :data="list"
    >
      <el-table-column label="序号" align="center" type="index" fixed="left" width="80" />
      <template v-for="(item, index) in tableTitleList">
        <el-table-column
          v-if="item.isTable && item.label != '序号'"
          :key="index"
          :type="item.type"
          :min-width="item.width"
          :header-align="item.headerAlign"
          :align="item.align"
          :label="item.label"
          :prop="item.field"
        >
          <template #default="scope">
            <dict-tag v-if="item.dictType" :type="item.dictType" :value="scope.row[item.field]" />
              <span v-else>{{ scope.row[item.field] }}</span>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts" name="outsourcingDeliveryNoteForm">
  import * as outsourcingDeliveryNoteApi from '@/api/outsource/outsourceInAndOutStorage/outsourcingDeliveryNote'
  import { useTagsViewStore } from '@/store/modules/tagsView'
  import { useUserStore } from '@/store/modules/user'
  const { push } = useRouter()
  const tagsViewStore = useTagsViewStore()
  const routes = useRouter() // 路由
  const total = ref(0) // 列表的总页数
  const list = ref<any[]>([]) // 列表的数据
  const tableTitleList = ref<CrudSchema[]>([]) // 表头列表
  const multipleTableRef = ref()
  const loading = ref(false) // 列表的加载中

  const queryParams = reactive({
    pageNo: 1,
    pageSize: 50,
    orderNumber: null,
    supplierName: null,
    salesName: null,
    requisitionForm: null,
    productNumber: null,
    partName: null,
    drawingNumber: null,
    arriveNumber: null,
    requirementTime: [],
    arriveNumber: undefined,
    signer: undefined,
    arriveTime: undefined
  })
  const queryFormRef = ref() // 搜索的表单
  const getSummaries = (param) => {
    const { columns, data } = param
    const sums: string[] = []
    columns.forEach((column, index) => {
      if (index === 0) {
        sums[index] = '总计'
        return
      }
      if (['arriveQuantity'].includes(column.property)) {
        const values = data.map((item) => Number(item[column.property]))
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return (parseFloat(prev) + parseFloat(curr)).toFixed(2)
            } else {
              return parseFloat(prev).toFixed(2)
            }
          }, 0)
        } else {
          sums[index] = ''
        }
      } else if (['purchaseQuantity'].includes(column.property)) {
        const values = data.map((item) => Number(item[column.property]))
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return (parseFloat(prev) + parseFloat(curr)).toFixed(2)
            } else {
              return parseFloat(prev).toFixed(2)
            }
          }, 0)
        } else {
          sums[index] = ''
        }
      } else if (['quantityCollected'].includes(column.property)) {
        const values = data.map((item) => Number(item[column.property]))
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return (parseFloat(prev) + parseFloat(curr)).toFixed(2)
            } else {
              return parseFloat(prev).toFixed(2)
            }
          }, 0)
        } else {
          sums[index] = ''
        }
      }
    })

    return sums
  }
  const getList = async () => {
    loading.value = true
    try {
      const data = await outsourcingDeliveryNoteApi.getoutsourcingDeliveryNotePage(queryParams)
      tableTitleList.value = data.labels
      list.value = data.list
      total.value = data.total
      queryParams.signer = list.value[0].signer,
      queryParams.arriveTime= list.value[0].arriveTime
      queryParams
      loading.value = false
    } finally {
      loading.value = false
    }
  }
  // 关闭

  const close = () => {
    tagsViewStore.delView(routes.currentRoute.value)
    push('/outsource/outsource/warehousingOutbound/outsourcingDeliveryNote')
  }

  /** 初始化 **/
  const route = useRoute()
  const tableHeight = ref(0)
  onMounted(async () => {
    queryParams.arriveNumber = route.query.list
    const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
    tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 160
    getList()
    //获取签收人
    queryParams.signer = useUserStore().getUser.nickname
  })
</script>
